<template>
  <div class="yuangongfuzhi-list">
    <div class="content-container">
      <div class="search-box">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="姓名">
            <el-input v-model="searchForm.employeeName" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="工号">
            <el-input v-model="searchForm.employeeId" placeholder="请输入工号"></el-input>
          </el-form-item>
          <el-form-item label="原部门">
            <el-input v-model="searchForm.originalDepartment" placeholder="请输入部门"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="operation-box">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>

      <el-table 
        :data="tableData" 
        border 
        style="width: 100%"
        :header-cell-style="{background:'#F5F7FA',color:'#303133',fontWeight: 'bold'}"
      >
        <el-table-column prop="employeeName" label="员工姓名" width="120"></el-table-column>
        <el-table-column prop="employeeId" label="工号" width="140"></el-table-column>
        <el-table-column prop="originalDepartment" label="原部门" width="180"></el-table-column>
        <el-table-column prop="reinstatementType" label="复职类型" width="120"></el-table-column>
        <el-table-column prop="status" label="状态" width="120"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="220" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleView(scope.row)">查看</el-button>
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-box">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getEmployeeReinstatementList, deleteEmployeeReinstatement } from '@/api/employee-reinstatement'

export default {
  name: 'YuangongfuzhiList',
  data() {
    return {
      searchForm: {
        employeeName: '',
        employeeId: '',
        originalDepartment: ''
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      try {
        const params = {
          current: this.currentPage,
          size: this.pageSize,
          ...this.searchForm
        }
        const res = await getEmployeeReinstatementList(params)
        if (res.data.code === 200) {
          this.tableData = res.data.data.records
          this.total = res.data.data.total
        }
      } catch (error) {
        console.error('获取数据失败:', error)
      }
    },
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    resetSearch() {
      this.searchForm = {
        employeeName: '',
        employeeId: '',
        originalDepartment: ''
      }
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    handleAdd() {
      this.$router.push({name: 'Yuangongfuzhi', query: { type: 'add' }})
    },
    handleView(row) {
      this.$router.push({
        name: 'Yuangongfuzhi',
        query: { id: row.id, type: 'view' }
      })
    },
    handleEdit(row) {
      this.$router.push({
        name: 'Yuangongfuzhi',
        query: { id: row.id, type: 'edit' }
      })
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该记录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        const res = await deleteEmployeeReinstatement(row.id)
        if (res.data.code === 200) {
          this.$message.success('删除成功')
          this.fetchData()
        }
      } catch (error) {
        console.error('删除失败:', error)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.yuangongfuzhi-list {
  min-height: 100vh;
  background-color: #f0f2f5;
  padding: 20px;
  box-sizing: border-box;
}

.content-container {
  width: 280mm;
  margin: 0 auto;
  background: #fff;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  box-sizing: border-box;
}

.search-box {
  margin-bottom: 20px;
  padding: 20px;
  background: #f9f9f9;
  border-radius: 4px;
}

.operation-box {
  margin-bottom: 20px;
}

.pagination-box {
  margin-top: 20px;
  text-align: right;
}

.el-table {
  margin-bottom: 20px;
}
</style> 